package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.KongshiTimesDao;
import com.int_tech.MessageCodeAll.dao.ZhongshiTimesDao;
import com.int_tech.MessageCodeAll.dao.ZhuangcheTimesDao;
import com.int_tech.MessageCodeAll.entity.KongshiTimes;
import com.int_tech.MessageCodeAll.entity.ZhongshiTimes;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.TimesPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/29.
 */
@Component
public class CheckZhongshiGongshi {

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private ZhongshiTimesDao zhongshiTimesDao;

    @Autowired
    private KongshiTimesDao kongshiTimesDao;


    /**
     * 校验重驶工时
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkZhongshiGongshi() throws ParseException {

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(2,1);

//        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOverAndCarNumber(2,1,"043C4C155");

        for (Mession mession : messionList)
        {
            try {
                mession.setIsAnsyCarTime(3);
                messionDao.save(mession);

                //装车结束时间
                Date startCloseTime = mession.getStartCloseDoorTime();

                //卸车开始时间
                Date endOpenTime = mession.getEndOpenDoorTime();


                //各个时间字符串日期
                String startCloseDay = DateTool.Date2String(startCloseTime,"yyyy-MM-dd");
                String endOpenDay = DateTool.Date2String(endOpenTime,"yyyy-MM-dd");

                //如果订单在同一天
                if(startCloseDay.equals(endOpenDay))
                {
                    float plus = 0;
                    plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndOpenDoorTime(),mession.getStartCloseDoorTime(),1));

                    if("0".equals(mession.getRate()))
                    {
                        KongshiTimes kongshiTimes = new KongshiTimes();

                        kongshiTimes.setCarNumber(mession.getCarNumber());
                        kongshiTimes.setCreatedTime(new Date());
                        kongshiTimes.setDateString(endOpenDay);
                        kongshiTimes.setMessionNumber(mession.getMessionNumber());

                        kongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        kongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        kongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        kongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        kongshiTimes.setStatus(0);

                        kongshiTimes.setKongshiTime(plus);

                        kongshiTimesDao.save(kongshiTimes);
                    }
                    else
                    {
                        ZhongshiTimes zhongshiTimes = new ZhongshiTimes();

                        zhongshiTimes.setCarNumber(mession.getCarNumber());
                        zhongshiTimes.setCreatedTime(new Date());
                        zhongshiTimes.setDateString(endOpenDay);
                        zhongshiTimes.setMessionNumber(mession.getMessionNumber());

                        zhongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        zhongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        zhongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        zhongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        zhongshiTimes.setStatus(0);

                        zhongshiTimes.setZhongshiTime(plus);

                        zhongshiTimesDao.save(zhongshiTimes);
                    }
                }
                else
                {
                    String startEndTime = startCloseDay + " 23:59:59";
                    String endStartTIme = endOpenDay + " 00:00:00";

                    Date dStartEndTime = DateTool.string2Date(startEndTime,"yyyy-MM-dd HH:mm:ss");
                    Date dEndStartTime = DateTool.string2Date(endStartTIme,"yyyy-MM-dd HH:mm:ss");

                    if("0".equals(mession.getRate()))
                    {
                        //第一天工时
                        float plus = 0;
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getStartCloseDoorTime(),1));

                        KongshiTimes kongshiTimes = new KongshiTimes();

                        kongshiTimes.setCarNumber(mession.getCarNumber());
                        kongshiTimes.setCreatedTime(new Date());
                        kongshiTimes.setDateString(startCloseDay);
                        kongshiTimes.setMessionNumber(mession.getMessionNumber());

                        kongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        kongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        kongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        kongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        kongshiTimes.setStatus(0);

                        kongshiTimes.setKongshiTime(plus);

                        kongshiTimesDao.save(kongshiTimes);

                        //第二天工时
                        plus = 0;
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndOpenDoorTime(),dEndStartTime,1));

                        kongshiTimes = new KongshiTimes();

                        kongshiTimes.setCarNumber(mession.getCarNumber());
                        kongshiTimes.setCreatedTime(new Date());
                        kongshiTimes.setDateString(endOpenDay);
                        kongshiTimes.setMessionNumber(mession.getMessionNumber());

                        kongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        kongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        kongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        kongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        kongshiTimes.setStatus(0);

                        kongshiTimes.setKongshiTime(plus);

                        kongshiTimesDao.save(kongshiTimes);
                    }
                    else
                    {
                        //第一天工时
                        float plus = 0;
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getStartCloseDoorTime(),1));

                        ZhongshiTimes zhongshiTimes = new ZhongshiTimes();

                        zhongshiTimes.setCarNumber(mession.getCarNumber());
                        zhongshiTimes.setCreatedTime(new Date());
                        zhongshiTimes.setDateString(startCloseDay);
                        zhongshiTimes.setMessionNumber(mession.getMessionNumber());

                        zhongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        zhongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        zhongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        zhongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        zhongshiTimes.setStatus(0);

                        zhongshiTimes.setZhongshiTime(plus);

                        zhongshiTimesDao.save(zhongshiTimes);

                        //第二天工时

                        plus = 0;
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndOpenDoorTime(),dEndStartTime,1));

                        zhongshiTimes = new ZhongshiTimes();

                        zhongshiTimes.setCarNumber(mession.getCarNumber());
                        zhongshiTimes.setCreatedTime(new Date());
                        zhongshiTimes.setDateString(endOpenDay);
                        zhongshiTimes.setMessionNumber(mession.getMessionNumber());

                        zhongshiTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                        zhongshiTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                        zhongshiTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                        zhongshiTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                        zhongshiTimes.setStatus(0);

                        zhongshiTimes.setZhongshiTime(plus);

                        zhongshiTimesDao.save(zhongshiTimes);
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();

                //装车开关门工时计算异常
                mession.setIsAnsyCarTime(-3);
                messionDao.save(mession);
            }
        }
    }
}
